/**
 * This file is part of the DbUnit module for NetBeans IDE
 * Copyright (C) 2009 Pavol Pitoňák
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

package cz.muni.fi.dbunit;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.border.EtchedBorder;
import org.openide.nodes.Node;
import org.openide.nodes.NodeAcceptor;

/**
 *
 * @author Pavol Pitoňák <pavol@pitonak.com>
 */
public class DataSetChooser extends javax.swing.JPanel {

    public static final String IS_VALID = "dataSetChooser_isValid"; //NOI18N
    private NodeAcceptor nodeAcceptor;
    private DataSetNodePanel dataSetNodePanel;

    /** Creates new form DataSetChooser */
    public DataSetChooser(Node root) {
        initComponents();

        nodeAcceptor = new NodeAcceptorImpl();

        dataSetNodePanel = new DataSetNodePanel(root);
        dataSetNodePanel.setBorder(new EtchedBorder());

        beanTreePanel.add(dataSetNodePanel);
        dataSetNodePanel.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent pce) {
                validateNodes();
            }
        });

        validateNodes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataSetLabel = new javax.swing.JLabel();
        beanTreePanel = new javax.swing.JPanel();

        dataSetLabel.setText(org.openide.util.NbBundle.getMessage(DataSetChooser.class, "DataSetChooser.dataSetLabel.text")); // NOI18N

        beanTreePanel.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(beanTreePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                    .addComponent(dataSetLabel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dataSetLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(beanTreePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel beanTreePanel;
    private javax.swing.JLabel dataSetLabel;
    // End of variables declaration//GEN-END:variables

    private void validateNodes() {
        boolean nodeAccepted = nodeAcceptor.acceptNodes(dataSetNodePanel.getSelectedNodes());
        if (!nodeAccepted) {
            firePropertyChange(IS_VALID, true, false);
        } else {
            firePropertyChange(IS_VALID, false, true);
        }
    }

    public String getFileName() {
        return dataSetNodePanel.getFileName();
    }

    private class NodeAcceptorImpl implements NodeAcceptor {

        public boolean acceptNodes(Node[] nodes) {
            if (nodes.length != 1) {
                return false;
            }
            if (nodes[0].getDisplayName().endsWith(".xml") || nodes[0].getDisplayName().endsWith(".XML")) {
                return true;
            } else {
                return false;
            }
        }
    }
}
